package com.ty.game.config;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import java.io.Serializable;
import java.util.Properties;

/**
 * config配置加载
 *
 * @author: Troy.Chen(失足程序员, 15388152619)
 * @version: 2021-07-29 14:07
 **/
public class YmlLoader implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 解析成yml格式
     */
    public static PropertySourcesPlaceholderConfigurer propertiesYml(String path) {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setProperties(properties(path));
        return propertySourcesPlaceholderConfigurer;
    }

    /**
     * 解析成 Properties
     *
     * @param path
     * @return
     */
    public static Properties properties(String path) {
        Resource resource = new FileSystemResource(path);
        if (!resource.exists()) {
            resource = new ClassPathResource(path);
        }
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(resource);
        Properties yamlObject = yaml.getObject();
        return yamlObject;
    }

}
